package com.hegongda.okcmall.product.service.impl;

import com.hegongda.okcmall.product.vo.ItemVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hegongda.common.utils.PageUtils;
import com.hegongda.common.utils.Query;

import com.hegongda.okcmall.product.dao.SkuInfoDao;
import com.hegongda.okcmall.product.entity.SkuInfoEntity;
import com.hegongda.okcmall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    /*
     * key=%E9%98%BF&
     * catelogId=225&
     * brandId=8&
     * min=5&
     * max=10
     */
    @Override
    public PageUtils querySkuInfoByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)){
            queryWrapper.and(obj -> obj.eq("sku_id",key).or().like("sku_name",key));
        }
        String catelogId = (String) params.get("catelogId");
        if (StringUtils.isNotEmpty(catelogId) && !catelogId.equals("0")){
            queryWrapper.eq("catalog_id", catelogId);
        }

        String brandId = (String) params.get("brandId");
        if (StringUtils.isNotEmpty(brandId) && !brandId.equals("0")){
            queryWrapper.eq("brand_id", brandId);
        }

        String min = (String) params.get("min");
        BigDecimal tempMin = new BigDecimal(min);
        if (StringUtils.isNotEmpty(min) && tempMin.compareTo(new BigDecimal(0)) == 1){
            queryWrapper.gt("price", min);
        }

        String max = (String) params.get("max");
        BigDecimal tempMax = new BigDecimal(max);
        if (StringUtils.isNotEmpty(max) && tempMax.compareTo(new BigDecimal(0)) == 1){
            queryWrapper.lt("price", max);
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> selectSkuBySpuId(Long spuId) {
        return baseMapper.selectList(new QueryWrapper<SkuInfoEntity>().eq("spu_id",spuId));
    }

    /**
     *  查询Sku的详细信息
     * @param skuId
     * @return
     */
    @Override
    public ItemVo getItemDetailBySkuId(String skuId) {


        return null;
    }

}